﻿Imports System.Data.SqlClient
Imports System.IO

Public Class frmClaimsFollowUp

    Sub FillClaims()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim StrCond As String = ""

            If Me.RMotor.Checked = True Then
                StrCond = " and ClaimType=N'MOT'"

            ElseIf Me.RMedical.Checked = True Then
                StrCond = " and ClaimType=N'MED'"

            ElseIf Me.RGeneral.Checked = True Then
                StrCond = " and ClaimType=N'GEN'"
            ElseIf Me.RAllClaimsTypes.Checked = True Then
                StrCond = ""
            Else
                Me.GridClaims.Rows.Clear()
                Me.Cursor = Cursors.Default
                Exit Sub
            End If

            Dim cmd As New SqlCommand("Select SNo,ClientID,ClientName,ClaimNo,Status,InsuranceCompany,PolicyNo,MembName,MedIDCarNo,ClaimAmount," & _
                                      "DateOfLoss,DateOfReceive,DateOfSubmission,DateOfDeadline,IsNull(InsurCompClaimNo,N'') InsurCompClaimNo," & _
                                      "IsNull(Notes,N'') Notes,SavedUser,SavedDate From Claims Where Status In (N'Active',N'Pending')" & StrCond, cnn)
            Dim Reader As SqlDataReader

            Me.GridClaims.Rows.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While (Reader.Read)
                Me.GridClaims.Rows.Add(New String() {Reader.Item("SNo"), Reader.Item("ClaimNo"), Reader.Item("Status"), Reader.Item("ClientID"), _
                                                     Reader.Item("ClientName"), Reader.Item("InsuranceCompany"), Reader.Item("PolicyNo"), _
                                                     Reader.Item("MembName"), Reader.Item("MedIDCarNo"), _
                                                     CDbl(Reader.Item("ClaimAmount")).ToString("N2"), _
                                                     Format(CDate(Reader.Item("DateOfLoss")), "yyyy/MM/dd"), _
                                                     Format(CDate(Reader.Item("DateOfReceive")), "yyyy/MM/dd"), _
                                                     Format(CDate(Reader.Item("DateOfSubmission")), "yyyy/MM/dd"), _
                                                     Format(CDate(Reader.Item("DateOfDeadline")), "yyyy/MM/dd"), _
                                                     Reader.Item("InsurCompClaimNo"), Reader.Item("Notes"), Reader.Item("SavedUser"), _
                                                     Format(CDate(Reader.Item("SavedDate")), "yyyy/MM/dd")})
            End While
            cnn.Close()

            'Check Colors
            For Each row As DataGridViewRow In Me.GridClaims.Rows
                If row.Cells(2).Value = "Active" Then
                    row.Cells(2).Style.BackColor = Color.Yellow

                ElseIf row.Cells(2).Value = "Settled" Then
                    row.Cells(2).Style.BackColor = Color.GreenYellow

                ElseIf row.Cells(2).Value = "Rejected" Then
                    row.Cells(2).Style.BackColor = Color.Red

                ElseIf row.Cells(2).Value = "Pending" Then
                    row.Cells(2).Style.BackColor = Color.Orange

                ElseIf row.Cells(2).Value = "Special Approval" Then
                    row.Cells(2).Style.BackColor = Color.LightSteelBlue
                End If
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmClaimsFollowUp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        FillClaims()
        FillUsersList()
    End Sub

    Sub FillUsersList()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select FullName From Users Order By FullName", cnn)
            Dim Reader As SqlDataReader

            Me.ChListContacts.Items.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.ChListContacts.Items.Add(Reader.Item("FullName"))
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub GridClaims_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridClaims.CurrentCellChanged
        Try
            Me.Cursor = Cursors.WaitCursor

            If Me.GridClaims.CurrentRow Is Nothing Then
                Me.Cursor = Cursors.Default
            Else
                Me.txtClaimNo.Text = Me.GridClaims.CurrentRow.Cells(1).Value
                FillClaimsNotes()
                FillClaimsPayments()
                FillClaimsDocuments()
                FillClaimsRejectedPayments()
            End If

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillClaimsNotes()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From ClaimsNotes Where ClaimNo=" & Me.GridClaims.CurrentRow.Cells(0).Value & "Order By SNo", cnn1)
            Dim Reader As SqlDataReader

            Me.txtProgressNotes.Clear()

            Dim ListHeader, ListNotes As New ArrayList

            cnn1.Open()
            Reader = cmd.ExecuteReader
            While (Reader.Read)
                Me.txtProgressNotes.Text += Reader.Item("SavedUser") & "     " & Format(CDate(Reader.Item("SavedDate")), "yyyy/MM/dd hh:mm:ss tt") & _
                                        Chr(13) & Reader.Item("Notes") & Chr(13) & Chr(13)

                ListHeader.Add(Reader.Item("SavedUser") & "     " & Format(CDate(Reader.Item("SavedDate")), "yyyy/MM/dd hh:mm:ss tt"))
                ListNotes.Add(Reader.Item("Notes"))
            End While
            cnn1.Close()

            For Each T As String In ListHeader
                txtProgressNotes.SelectionStart = txtProgressNotes.Find(T)
                Dim bfont As New Font(txtProgressNotes.Font, FontStyle.Bold)
                txtProgressNotes.SelectionFont = bfont
                txtProgressNotes.SelectionColor = Color.Blue
            Next

            Me.txtProgressNotes.ScrollToCaret()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn1.State = ConnectionState.Open Then
                cnn1.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillClaimsPayments()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From ClaimsPayments Where ClaimNo=" & Me.GridClaims.CurrentRow.Cells(0).Value, cnn2)
            Dim Reader As SqlDataReader

            Me.GridPayments.Rows.Clear()

            cnn2.Open()
            Reader = cmd.ExecuteReader
            While (Reader.Read)
                Me.GridPayments.Rows.Add(New String() {CDbl(Reader.Item("Amount")).ToString("N2"), Reader.Item("PaymentDetails"), _
                                                       Reader.Item("SavedUser"), Format(CDate(Reader.Item("SavedDate")), "yyyy/MM/dd")})
            End While
            cnn2.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn2.State = ConnectionState.Open Then
                cnn2.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillClaimsRejectedPayments()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From ClaimsRejections Where ClaimNo=" & Me.GridClaims.CurrentRow.Cells(0).Value, cnn3)
            Dim Reader As SqlDataReader

            Me.GridRejectedPayments.Rows.Clear()

            cnn3.Open()
            Reader = cmd.ExecuteReader
            While (Reader.Read)
                Me.GridRejectedPayments.Rows.Add(New String() {CDbl(Reader.Item("Amount")).ToString("N2"), Reader.Item("RejectionReason"), _
                                                       Reader.Item("SavedUser"), Format(CDate(Reader.Item("SavedDate")), "yyyy/MM/dd")})
            End While
            cnn3.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn3.State = ConnectionState.Open Then
                cnn3.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillClaimsDocuments()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim Dir As New IO.DirectoryInfo(FTPAddress + "Claims\" + Me.GridClaims.CurrentRow.Cells(1).Value)
            Dim DirFiles As IO.FileInfo() = Dir.GetFiles()

            Me.GridFiles.Rows.Clear()

            'list the names of all files in the specified directory
            For Each Fi As FileInfo In DirFiles
                Me.GridFiles.Rows.Add(New String() {Fi.Name, Fi.CreationTime.ToString("yyyy/MM/dd hh:mm tt"), "Open", "Delete"})
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub GridFiles_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridFiles.CellClick
        If e.ColumnIndex = 2 Then
            'Dim Fi As New FileInfo(FTPAddress + "Claims\" + ClaimNo + "\" + Me.GridFiles.Rows(e.RowIndex).Cells(0).Value)
            'Fi.Open(FileMode.Open, FileAccess.ReadWrite)
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim FilePath As String = FTPAddress + "Claims\" + Me.GridClaims.CurrentRow.Cells(1).Value + "\" + Me.GridFiles.Rows(e.RowIndex).Cells(0).Value
                System.Diagnostics.Process.Start(FilePath)

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
        SaveNotes()
    End Sub

    Sub SaveNotes()
        If Me.GridClaims.Rows.Count > 0 Then
            If Me.txtNotes.Text.Trim.Length = 0 Then
                MsgBox("Please enter your message")
                Me.txtNotes.Clear()
                Me.txtNotes.Focus()
                Exit Sub
            Else
                Try
                    Me.Cursor = Cursors.WaitCursor
                    Dim cmd As New SqlCommand("Insert Into ClaimsNotes (ClaimNo,Notes,SavedUser) Values " & _
                                              "(@ClaimNo,@Notes,@SavedUser)", cnn)
                    cnn.Open()

                    'Add values
                    cmd.Parameters.AddWithValue("@ClaimNo", Me.GridClaims.CurrentRow.Cells(0).Value)
                    cmd.Parameters.AddWithValue("@Notes", Me.txtNotes.Text.Trim)
                    cmd.Parameters.AddWithValue("@SavedUser", CurrentUser)

                    cmd.ExecuteNonQuery()
                    cnn.Close()

                    'Send notification
                    Dim Subject, Body As String
                    Dim Receivers As New ArrayList

                    If Me.ChListContacts.CheckedItems.Count > 0 Then
                        Subject = "Oasis - IBS Notification - Claims Management System"
                        Body = "Your received a notification from: <B>" & CurrentUser & _
                               "</B>, regarding claim # <B>" & Me.GridClaims.CurrentRow.Cells(1).Value & "</B><br/><br/>" & _
                               "Please don't reply to this email.<br/>" & _
                               "Sent from Oasis - Insurance Broker System"
                        For i As Integer = 0 To Me.ChListContacts.Items.Count - 1
                            If Me.ChListContacts.GetItemChecked(i) Then
                                Receivers.Add(GetUserEmail(Me.ChListContacts.Items(i)))
                            End If
                        Next

                        SendEmail(Subject, Body, Receivers)

                        MsgBox("Notification has been sent successfully")
                    End If

                    FillClaimsNotes()

                    Me.txtNotes.Clear()
                    Me.txtNotes.Focus()
                    For i As Integer = 0 To Me.ChListContacts.Items.Count - 1
                        Me.ChListContacts.SetItemChecked(i, False)
                    Next

                    Me.Cursor = Cursors.Default
                Catch ex As Exception
                    Me.Cursor = Cursors.Default
                    If cnn.State = ConnectionState.Open Then
                        cnn.Close()
                    End If
                    MsgBox(ex.ToString)
                End Try
            End If
        End If
    End Sub

    Private Sub txtNotes_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNotes.KeyUp
        If e.Shift And e.KeyCode = Keys.Enter Then
            Exit Sub
        ElseIf e.KeyCode = Keys.Enter Then
            SaveNotes()
        End If
    End Sub

    Private Sub RAllClaimsTypes_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RAllClaimsTypes.CheckedChanged
        FillClaims()
    End Sub

    Private Sub RGeneral_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RGeneral.CheckedChanged
        FillClaims()
    End Sub

    Private Sub RMedical_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RMedical.CheckedChanged
        FillClaims()
    End Sub

    Private Sub RMotor_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RMotor.CheckedChanged
        FillClaims()
    End Sub

    Private Sub BtnClose_Click(sender As System.Object, e As System.EventArgs)
        Me.Close()
    End Sub
End Class